﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ESMAM.DAL.SYS
{
    public class WordTypeDAL: ESMAM.DAL.DAO.DAL
    {
        public Pager GetPager(ReqParam req)
        {
            var kv = DbTop.GetDataTableByPager(req.CurrentPage, req.PageSize, "*", "sys_word_type ", req.Where, req.Orders);
            Pager pager = new Pager()
            {
                list = kv.Key,
                pageCurrent = req.CurrentPage,
                pageSize = req.PageSize,
                totalRow = kv.Value
            };
            return pager;
        }

        public KeyValuePair<bool, string> Upsert(NameValueCollection nvc)
        {
            var kv = DbTop.Save(nvc, "sys_word_type", "id");

            string msg = string.Empty;

            if (kv.Key == MJTop.Data.SaveType.Insert)
            {
                if (kv.Value)
                {
                    msg = "添加成功！";
                }
                else
                {
                    msg = "添加失败！";
                }
            }
            else
            {
                if (kv.Value)
                {
                    msg = "更新成功！";
                }
                else
                {
                    msg = "更新失败！";
                }
            }
            return new KeyValuePair<bool, string>(kv.Value, msg);
        }

        public bool Delete(string id)
        {
            string del_sql = string.Format("delete from sys_word_type where id ={0};", id);

            del_sql += "delete from sys_word where typeid=" + id;

            return DbTop.ExecSql(del_sql) > 0;
        }

    }
}
